#coding:utf-8
from __future__ import unicode_literals

from django.db import models
from django.contrib.auth.models import User

# Create your models here.

from django.core.mail import send_mail
from django.conf import settings
from django.contrib.auth.models import BaseUserManager, \
    AbstractBaseUser, PermissionsMixin
from datetime import datetime

from django.contrib.auth.models import Group

class Country(models.Model):
    country_name = models.CharField(max_length=200,verbose_name='英文名称')
    country_name_cn = models.CharField(max_length=200,verbose_name='中文名称')
    country_code = models.CharField(max_length=10,db_index=True,unique=True,verbose_name='国家简码')
    search_string = models.CharField(max_length=100,null=True,blank=True,default=None,verbose_name='搜索匹配字符串')

    class Meta:
        verbose_name = u'国家'
        verbose_name_plural = verbose_name
        db_table = 'country'

    def __unicode__(self):
        return self.country_name


class PlatformCategory(models.Model):
    platform = models.IntegerField(choices=((1,'速卖通'),(2,'eBay'),(3,'Wish'),(4,'亚马逊')),verbose_name=u'平台')
    name = models.CharField(max_length=200,unique=True,verbose_name=u'分类名称')
    category_id =  models.CharField(max_length=100,verbose_name=u'分类ID')
    url = models.CharField(max_length=500,verbose_name=u'URL')

    class Meta:
        verbose_name = u'平台分类'
        verbose_name_plural = verbose_name
        db_table = 'platform_category'
        unique_together = ("platform", "category_id")

    def __unicode__(self):
        return self.name


class Category(models.Model):
    parent_id = models.ForeignKey('self', null=True, blank=True, related_name='parent',verbose_name = u'上级分类')
    name = models.CharField(max_length=200,unique=True,verbose_name=u'类目名称')
    platform_category = models.ManyToManyField(PlatformCategory,verbose_name=u'类目ID')
    index = models.IntegerField(default=999,verbose_name = '分类排序')

    class Meta:
        verbose_name = u'类目'
        verbose_name_plural = verbose_name
        db_table = 'category'

    def __unicode__(self):
        return self.name


class SearchWords(models.Model):
    category = models.ForeignKey(Category,null=True,verbose_name=u'类目')
    keyword = models.CharField(max_length=200,unique=True,verbose_name="关键词")
    keyword_chinese = models.CharField(max_length=500,null=True,blank=True,verbose_name="关键词中文")
    url = models.CharField(max_length=500,blank=True,null=True,verbose_name="采集URL")
    status = models.BooleanField(default=False,verbose_name="采集状态")
    results = models.IntegerField(default=0,verbose_name="产品总数")
    product_num = models.IntegerField(default=0,verbose_name="采集产品数量")
    update_time = models.DateTimeField(null=True,blank=True,verbose_name="最后采集时间")
    add_time = models.DateTimeField(auto_now_add=True,null=True,blank=True,verbose_name="添加时间")
    order_filter = models.IntegerField(default=0,null=True,blank=True,verbose_name="过滤订单")
    user = models.ManyToManyField(User,verbose_name='用户')

    class Meta:
        verbose_name = u'搜索关键词'
        verbose_name_plural = verbose_name
        db_table = 'search_words'

    def __unicode__(self):
        return self.keyword


class Products(models.Model):
    product_id = models.CharField(db_index=True,unique=True,max_length=100,verbose_name=u"产品编号")
    product_title = models.CharField(max_length=500,null=True,verbose_name=u"产品标题")
    product_title_chinese = models.CharField(max_length=500,null=True,verbose_name=u"产品中文标题")
    category_id = models.CharField(max_length=100,null=True,verbose_name=u"产品分类ID")
    store_id = models.CharField(max_length=100,null=True,verbose_name=u"店铺ID")
    product_link = models.CharField(max_length=500,null=True,verbose_name=u"产品链接URL")
    thumb = models.CharField(max_length=500,null=True,verbose_name=u"产品主图URL")
    currency = models.CharField(max_length=10,null=True,blank=True,verbose_name=u'货币单位')
    high_price = models.DecimalField(max_digits=10,decimal_places=2,null=True,verbose_name=u"最高价格")
    low_price = models.DecimalField(max_digits=10,decimal_places=2,null=True,verbose_name=u"最低价格")
    discount = models.IntegerField(default=0,null=True,verbose_name=u"折扣")
    wish_list = models.IntegerField(default=0,null=True,verbose_name=u"心愿单")
    orders = models.IntegerField(default=0,null=True,verbose_name=u"总订单")
    votes = models.IntegerField(default=0,null=True,verbose_name=u"投票数")
    average_star_rating = models.DecimalField(max_digits=10,decimal_places=2,null=True,verbose_name=u"评价得分")
    status = models.SmallIntegerField(default=0,verbose_name=u"基本信息采集状态")
    status_trans = models.SmallIntegerField(default=0,verbose_name=u"交易记录采集状态")
    transactions_in_last_six_months = models.IntegerField(default=0,null=True,verbose_name=u"6个月交易数量")
    first_order_date = models.DateField(blank=True,null=True,verbose_name=u"首单时间")
    platform = models.IntegerField(null=True,blank=True,choices=((1,'速卖通'),(2,'eBay'),(3,'Wish'),(4,'亚马逊')),verbose_name=u'平台')
    free_shipping = models.BooleanField(default=False,verbose_name=u'是否包邮')
    weekly_orders = models.IntegerField(null=True,blank=True,verbose_name=u'周订单数')
    monthly_orders = models.IntegerField(null=True,blank=True,verbose_name=u'月订单数')
    quarter_orders = models.IntegerField(null=True,blank=True,verbose_name=u'季度订单数')
    top_sale_country = models.CharField(max_length=200,null=True,blank=True,verbose_name=u'主要售卖国家')
    monthly_growth_rates = models.DecimalField(max_digits=10,decimal_places=2,null=True,verbose_name=u"近30天增长率")
    fifteen_growth_rates = models.DecimalField(max_digits=10,decimal_places=2,null=True,verbose_name=u"近15天增长率")
    seven_growth_rates = models.DecimalField(max_digits=10,decimal_places=2,null=True,verbose_name=u"近7天增长率")
    three_growth_rates = models.DecimalField(max_digits=10,decimal_places=2,null=True,verbose_name=u"近3天增长率")
    sku_quantity = models.IntegerField(default=1,null=True,verbose_name=u"SKU数量")
    add_date = models.DateTimeField(auto_now_add=True,blank=True,null=True,verbose_name=u"添加时间")
    update_date = models.DateTimeField(auto_now=True,null=True,blank=True,verbose_name=u"最后操作时间")
    product_update_date = models.DateTimeField(default=None,null=True,blank=True,verbose_name=u"最后更新时间")
    trans_update_date = models.DateTimeField(default=None,null=True,blank=True,verbose_name=u"交易记录最后更新时间")
    keyword = models.ManyToManyField(SearchWords,verbose_name=u"搜索关键词")
    is_single = models.BooleanField(default=0,blank=True,verbose_name=u'是否为单条采集的产品')
    is_remove = models.BooleanField(default=0,blank=True,verbose_name=u'是否下架')
    is_new = models.SmallIntegerField(default=0,blank=True,verbose_name=u'是否新品')
    score = models.DecimalField(max_digits=10,decimal_places=2,default=0.0,null=True,verbose_name=u'权重分')
    lock = models.BooleanField(default=False,blank=True,verbose_name=u'是否锁住')
    monitor = models.BooleanField(default=False,blank=True,verbose_name=u'加入监控')
    monitor_user = models.ManyToManyField(User,related_name='monitor_user',verbose_name=u'监控用户')
    user = models.ManyToManyField(User,related_name='favarite_user',verbose_name=u'收藏用户')

    class Meta:
        verbose_name = u'产品列表'
        verbose_name_plural = verbose_name
        ordering = ['-orders','-trans_update_date']
        db_table = 'products'

    def __unicode__(self):
        return self.product_id


class Transaction(models.Model):
    product_id = models.CharField(max_length=100,verbose_name="产品编号")
    trans_id = models.CharField(max_length=100,db_index=True,unique=True,verbose_name="交易编号ID")
    evaluation_id = models.CharField(max_length=100,verbose_name="评价编号ID")
    username = models.CharField(max_length=100,verbose_name="买家用户名")
    country_code = models.CharField(max_length=100,verbose_name="国家代码")
    country_name = models.CharField(max_length=200,verbose_name="国家名称")
    buyer_leval = models.CharField(max_length=100,verbose_name="买家等级")
    price = models.DecimalField(max_digits=10,decimal_places=2,verbose_name="成交单价")
    quantity = models.IntegerField(default=0,verbose_name="数量")
    unit = models.CharField(max_length=100,verbose_name="单位")
    lot_num = models.IntegerField(default=0,verbose_name="批量数目")
    star = models.IntegerField(default=0,verbose_name="评价分数")
    pub_date = models.DateTimeField(default=None,null=True,verbose_name="成交时间")
    buyer_feedback = models.TextField(verbose_name="买家评价")
    supplier_reply = models.TextField(verbose_name="商家回复")
    buyer_reply = models.TextField(verbose_name="买家回复")
    digg_up = models.IntegerField(default=0,verbose_name="顶")
    digg_down = models.IntegerField(default=0,verbose_name="踩")
    has_digg = models.IntegerField(default=0,verbose_name="已顶")
    can_digg = models.BooleanField(default=False,verbose_name="是否可顶")

    class Meta:
        verbose_name = u'交易记录'
        verbose_name_plural = verbose_name
        db_table = 'transaction'

    def __unicode__(self):
        return self.username

class WordTrends(models.Model):
    word = models.ForeignKey(SearchWords,verbose_name='关键词')
    track_date = models.DateField(default=None,verbose_name='跟踪日期')
    update_time = models.DateTimeField(auto_now=True,verbose_name='更新时间')
    results = models.IntegerField(default=0,verbose_name="搜索结果数")
    product_num = models.IntegerField(default=0,verbose_name="采集产品数量")

    class Meta:
        verbose_name = u'关键词跟踪'
        verbose_name_plural = verbose_name
        db_table = 'word_trace'
        unique_together = ("word", "track_date")

    def __unicode__(self):
        return str(self.word)


class ProductTrends(models.Model):
    product = models.ForeignKey(Products,verbose_name='产品')
    track_date = models.DateField(default=None,verbose_name='跟踪日期')
    update_time = models.DateTimeField(auto_now=True,null=True,verbose_name='更新时间')
    product_title = models.CharField(max_length=500,null=True,verbose_name="产品标题")
    thumb = models.CharField(max_length=500,null=True,verbose_name="产品主图URL")
    high_price = models.DecimalField(max_digits=10,decimal_places=2,null=True,verbose_name="最高价格")
    low_price = models.DecimalField(max_digits=10,decimal_places=2,null=True,verbose_name="最低价格")
    discount = models.IntegerField(default=0,null=True,verbose_name="折扣")
    wish_list = models.IntegerField(default=0,null=True,verbose_name="心愿单")
    orders = models.IntegerField(default=0,null=True,verbose_name="总订单")
    votes = models.IntegerField(default=0,null=True,verbose_name="投票数")
    average_star_rating = models.DecimalField(max_digits=10,decimal_places=2,null=True,verbose_name="评价得分")

    class Meta:
        verbose_name = u'产品跟踪'
        verbose_name_plural = verbose_name
        db_table = 'product_trace'
        unique_together = ("product", "track_date")

    def __unicode__(self):
        return str(self.product)


class Logistics(models.Model):
    user = models.ForeignKey(User,null=True,blank=True,verbose_name='用户')
    channel = models.CharField(max_length=200,null=True,blank=True,verbose_name='物流渠道')
    company = models.CharField(max_length=200,null=True,blank=True,verbose_name='快递公司')
    exname = models.CharField(max_length=100,null=True,blank=True,verbose_name='快递公司简码')
    tracking_number = models.CharField(max_length=100,unique=True,verbose_name='物流运单号')
    send_time = models.DateTimeField(null=True,blank=True,verbose_name='发货时间')
    delivery_time = models.DateTimeField(null=True,blank=True,verbose_name='快递收件时间')
    update_time = models.DateTimeField(null=True,blank=True,verbose_name='包裹最后更新时间')
    query_time = models.DateTimeField(null=True,blank=True,verbose_name='最后查询时间')
    destination = models.CharField(max_length=100,blank=True,null=True,verbose_name='目的地')
    status = models.IntegerField(default=0,null=True,choices=((0,'未查询'),(1,'妥投'),(2,'已签收'),(3,'异常'),(4,'运输中'),(5,'查询不到'),(6,'跟踪结束'),(7,'运输过久'),(8,'投寄失败')),verbose_name='状态')
    num1 = models.IntegerField(default=0,null=True,verbose_name='运送时长')
    num2 = models.IntegerField(default=0,null=True,verbose_name='总运输时间')
    mark = models.BooleanField(default=False,verbose_name='是否处理')

    class Meta:
        verbose_name = u'物流跟踪'
        verbose_name_plural = verbose_name
        db_table = 'logistics'

    def __unicode__(self):
        return self.tracking_number


class LogisticsSetting(models.Model):
    user = models.ForeignKey(User,verbose_name='用户')
    channel = models.CharField(max_length=100,verbose_name='物流渠道')
    country = models.CharField(max_length=100,verbose_name='国家')
    average = models.IntegerField(default=0,verbose_name='平均签收时间')
    regulation = models.IntegerField(default=4,verbose_name='调控值')

    class Meta:
        verbose_name = u'物流监控设置'
        verbose_name_plural = verbose_name
        db_table = 'logistics_setting'

    def __unicode__(self):
        return self.channel


class UserProfile(models.Model):
    user = models.OneToOneField(User,verbose_name='用户名')
    name = models.CharField(max_length=100,verbose_name='姓名')
    avatar = models.ImageField(upload_to='avatar/%Y/%m', default='avatar/default_big.png', max_length=200, blank=True, null=True, verbose_name=u'头像220x220')
    tel = models.CharField(max_length=11,null=True,blank=True,verbose_name='手机')
    email = models.CharField(max_length=100,null=True,blank=True,verbose_name='邮箱')
    dxm_username = models.CharField(max_length=100,null=True,blank=True,verbose_name='店小秘用户名')
    dxm_password = models.CharField(max_length=100,null=True,blank=True,verbose_name='密码')
    wuliu_yujing = models.IntegerField(default=10,null=True,blank=True,verbose_name='物流预警')
    wuliu_yichang = models.IntegerField(default=30,null=True,blank=True,verbose_name='物流异常')


    class Meta:
        verbose_name = u'用户信息'
        verbose_name_plural = verbose_name
        db_table = 'user_profile'

    def __unicode__(self):
        return self.name


class LogisticsCollectRecord(models.Model):
    start_date = models.DateTimeField(auto_now_add=True,verbose_name=u'开始时间')
    total = models.IntegerField(verbose_name=u'结果数')
    complete = models.IntegerField(default=0,verbose_name=u'完成数量')
    end_date = models.DateTimeField(null=True,blank=True,verbose_name=u'结束时间')

    class Meta:
        verbose_name = u'物流采集记录'
        verbose_name_plural = verbose_name
        db_table = 'logistics_collect_record'

    def __unicode__(self):
        return str(self.id)


class Notice(models.Model):
    members = models.IntegerField(default=0,choices=((0,u'所有用户'),(1,u'VIP用户'),(2,u'普通用户'),(3,u'试用用户')),verbose_name=u'通知对象')
    category = models.IntegerField(default=0,verbose_name=u'类型')
    title = models.CharField(max_length=200,verbose_name=u'标题')
    content = models.TextField(default=None,verbose_name=u'内容')
    link = models.CharField(max_length=300,null=True,blank=True,default=None,verbose_name=u'链接')
    send_time = models.DateTimeField(auto_now_add=True,verbose_name=u'发送时间')

    class Meta:
        verbose_name = u'公告'
        verbose_name_plural = verbose_name
        db_table = 'notice'

    def __unicode__(self):
        return self.title


class NoticeMark(models.Model):
    notice = models.ForeignKey(Notice,verbose_name=u'通知')
    user = models.ForeignKey(User,verbose_name=u'用户')
    is_read = models.BooleanField(default=False,verbose_name=u'是否阅读')
    label = models.CharField(max_length=100,null=True,blank=True,verbose_name=u'自定义标签')

    class Meta:
        verbose_name = u'公告标记'
        verbose_name_plural = verbose_name
        db_table = 'notice_mark'
        unique_together = ("notice", "user")

    def __unicode__(self):
        return str(self.id)

class Store(models.Model):
    platform = models.IntegerField(null=True,blank=True,choices=((1,'速卖通'),(2,'eBay'),(3,'Wish'),(4,'亚马逊')),verbose_name=u'平台')
    store_id = models.CharField(max_length=100,unique=True,verbose_name=u'店铺ID')
    store_link = models.CharField(max_length=200,null=True,verbose_name=u'店铺链接')
    seller = models.CharField(max_length=100,null=True,verbose_name=u'卖家')
    feedback_score = models.IntegerField(default=0,null=True,verbose_name=u'卖家信用')
    positive_feedback = models.DecimalField(max_digits=5,decimal_places=2,null=True,verbose_name=u"好评率")
    item_as_described = models.DecimalField(max_digits=2,decimal_places=1,null=True,verbose_name=u"描述相符")
    communication = models.DecimalField(max_digits=2,decimal_places=1,null=True,verbose_name=u"服务态度")
    shipping_speed = models.DecimalField(max_digits=2,decimal_places=1,null=True,verbose_name=u"物流服务")
    open_time = models.DateField(null=True,blank=True,verbose_name=u'开店日期')
    open_year = models.IntegerField(default=0,null=True,verbose_name=u'开店时长')
    sale_items = models.IntegerField(default=0,null=True,verbose_name=u'产品数量')
    new_arrivals_items = models.IntegerField(default=0,null=True,verbose_name=u'15天上架新品')
    new_selling_items = models.IntegerField(default=0,null=True,verbose_name=u'15天出单新品')
    monitor_user = models.ManyToManyField(User,related_name='monitor_store_user',verbose_name=u'监控用户')
    manage_user = models.ManyToManyField(User,related_name='manage_store_user',verbose_name=u'管理用户')
    add_time = models.DateTimeField(auto_now_add=True,null=True,blank=True,verbose_name=u'添加时间')
    update_time = models.DateTimeField(default=None,null=True,blank=True,verbose_name=u'更新时间')
    status = models.SmallIntegerField(default=0,null=True,blank=True,verbose_name=u'状态')

    class Meta:
        verbose_name = u'店铺'
        verbose_name_plural = verbose_name
        db_table = 'store'

    def __unicode__(self):
        return self.seller


class Message(models.Model):
    sender = models.ForeignKey(User,related_name='sender',null=True,blank=True,verbose_name=u'发件人')
    receiver = models.ManyToManyField(User,related_name='receiver',verbose_name=u'收件人')
    title = models.CharField(max_length=200,verbose_name=u'标题')
    content = models.TextField(verbose_name=u'内容')
    send_time = models.DateTimeField(auto_now_add=True,verbose_name=u'时间')

    class Meta:
        verbose_name = u'站内消息'
        verbose_name_plural = verbose_name
        db_table = 'message'
        ordering = ['-send_time']

    def __unicode__(self):
        return self.title

class MessageMark(models.Model):
    message = models.ForeignKey(Message,verbose_name=u'通知')
    user = models.ForeignKey(User,verbose_name=u'用户')
    is_read = models.BooleanField(default=False,verbose_name=u'是否阅读')
    label = models.CharField(max_length=100,null=True,blank=True,verbose_name=u'自定义标签')

    class Meta:
        verbose_name = u'消息标记'
        verbose_name_plural = verbose_name
        db_table = 'message_mark'
        unique_together = ("message", "user")

    def __unicode__(self):
        return str(self.id)

class StoreTrends(models.Model):
    store = models.ForeignKey(Store,verbose_name=u'店铺')
    track_date = models.DateField(default=None,verbose_name=u'跟踪日期')
    update_time = models.DateTimeField(auto_now=True,null=True,verbose_name='更新时间')
    sale_items = models.IntegerField(default=0,null=True,verbose_name=u'产品总数')
    new_selling_items = models.IntegerField(default=0,null=True,verbose_name=u'上新品数量')

    class Meta:
        verbose_name = u'店铺跟踪'
        verbose_name_plural = verbose_name
        db_table = 'store_trace'
        unique_together = ("store", "track_date")

    def __unicode__(self):
        return str(self.store)

class Supplier(models.Model):
    url = models.CharField(max_length=300,verbose_name=u'商品URL')
    title = models.CharField(max_length=200,null=True,blank=True,verbose_name=u'商品标题')
    seller = models.CharField(max_length=100,null=True,blank=True,verbose_name=u'卖家')
    thumb = models.CharField(max_length=200,null=True,blank=True,verbose_name=u'主图')

    class Meta:
        verbose_name = u'供应商'
        verbose_name_plural = verbose_name
        db_table = 'supplier'

    def __unicode__(self):
        return self.title


class NewCategory(models.Model):
    name = models.CharField(max_length=100,verbose_name=u'名称')
    index = models.IntegerField(default=99,verbose_name=u'排序')

    class Meta:
        verbose_name = u'文章分类'
        verbose_name_plural = verbose_name
        db_table = 'new_category'

    def __unicode__(self):
        return self.name


class Article(models.Model):
    title = models.CharField(max_length=300,verbose_name=u'标题')
    category = models.ForeignKey(NewCategory,null=True,blank=True,verbose_name=u'分类')
    content = models.TextField(verbose_name=u'内容')
    pub_date = models.DateTimeField(verbose_name=u'发布时间')

    class Meta:
        verbose_name = u'文章'
        verbose_name_plural = verbose_name
        db_table = 'article'

    def __unicode__(self):
        return self.title


class Competitor(models.Model):
    my_product = models.ForeignKey(Products,related_name='my_product',verbose_name=u'我的产品')
    mo_product = models.ForeignKey(Products,related_name='mo_product',verbose_name=u'竞品')
    user = models.ForeignKey(User,null=True,related_name='Competitor_user',verbose_name=u'用户')

    class Meta:
        verbose_name = u'竞品绑定'
        verbose_name_plural = verbose_name
        db_table = 'competitor'

    def __unicode__(self):
        return str(self.id)


class Configure(models.Model):
    key = models.CharField(max_length=100,unique=True,verbose_name=u'参数')
    value = models.CharField(max_length=100,verbose_name=u'值')

    class Meta:
        verbose_name = u'配置参数'
        verbose_name_plural = verbose_name
        db_table = 'configure'

    def __unicode__(self):
        return self.key

class Package(models.Model):
    product = models.OneToOneField(Products,verbose_name='产品')
    weight = models.IntegerField(null=True,blank=True,verbose_name=u'重量')
    length = models.IntegerField(null=True,blank=True,verbose_name=u'长')
    width = models.IntegerField(null=True,blank=True,verbose_name=u'宽')
    height = models.IntegerField(null=True,blank=True,verbose_name=u'高')
    update_date = models.DateField(null=True,blank=True,verbose_name=u'更新日期')

    class Meta:
        verbose_name = u'包裹参数'
        verbose_name_plural = verbose_name
        db_table = 'package'

    def __unicode__(self):
        return str(self.id)


class PermissionType(models.Model):
    name = models.CharField(max_length=255,verbose_name=u'名称')
    order = models.IntegerField(default=99,verbose_name=u'排序')

    class Meta:
        verbose_name = u'权限分类'
        verbose_name_plural = verbose_name
        db_table = 'sys_permission_type'

    def __unicode__(self):
        return self.name

class Permission(models.Model):
    name = models.CharField(max_length=255,verbose_name=u'权限名称')
    codename = models.CharField(max_length=100,unique=True,verbose_name=u'代码')
    description = models.TextField(null=True,blank=True,default=None,verbose_name=u'说明')
    permission_type = models.ForeignKey(PermissionType,default=None,null=True,blank=True,verbose_name=u'权限分类')

    class Meta:
        verbose_name = u'权限表'
        verbose_name_plural = verbose_name
        db_table = 'sys_permission'

    def __unicode__(self):
        return self.name


class GroupPermission(models.Model):
    group = models.OneToOneField(Group,unique=True,verbose_name=u'用户组')
    permission = models.CharField(max_length=200,null=True,blank=True,default=None,verbose_name=u'权限')
    #permission = models.ForeignKey(Permission,verbose_name=u'权限')

    class Meta:
        verbose_name = u'分组权限表'
        verbose_name_plural = verbose_name
        db_table = 'sys_group_permission'
        unique_together = ("group", "permission")

    def __unicode__(self):
        return str(self.id)

class UserPermission(models.Model):
    user = models.OneToOneField(User,unique=True,verbose_name=u'用户')
    permission = models.CharField(max_length=200,null=True,blank=True,default=None,verbose_name=u'权限')
    #permission = models.ForeignKey(Permission,verbose_name=u'权限')

    class Meta:
        verbose_name = u'用户权限表'
        verbose_name_plural = verbose_name
        db_table = 'sys_user_permission'
        unique_together = ("user", "permission")

    def __unicode__(self):
        return str(self.id)


class TranslateContent(models.Model):
    md5 = models.CharField(max_length=100,unique=True,verbose_name=u'原文MD5')
    src = models.TextField(null=True,blank=True,default=None,verbose_name=u'原文')
    dst = models.TextField(null=True,blank=True,default=None,verbose_name=u'译文')

    class Meta:
        verbose_name = u'消息翻译对照表'
        verbose_name_plural = verbose_name
        db_table = 'translate_content'

    def __unicode__(self):
        return self.md5


class ProductDeclare(models.Model):
    order_id = models.CharField(max_length=50,null=True,blank=True,verbose_name=u'订单编号')
    product_id = models.TextField(verbose_name=u'产品ID')
    chinese_name = models.CharField(max_length=100,verbose_name=u'中文名称')
    english_name = models.CharField(max_length=100,verbose_name=u'英文名称')
    amount = models.DecimalField(max_digits=10,decimal_places=2,default=2.00,null=True,verbose_name=u"申报金额")
    currency = models.CharField(max_length=10,default='USD',verbose_name=u"申报币种")
    weight = models.IntegerField(default=100,null=True,verbose_name=u"重量")
    code = models.CharField(max_length=20,default=None,blank=True,null=True,verbose_name=u"海关编码")

    class Meta:
        verbose_name = u'产品申报信息'
        verbose_name_plural = verbose_name
        db_table = 'product_declare'

    def __unicode__(self):
        return self.chinese_name

class StoreInfo(models.Model):
    store_id = models.IntegerField(verbose_name=u'店铺ID')
    name = models.CharField(max_length=100,verbose_name=u'店铺名称')
    children = models.CharField(max_length=200,verbose_name=u'店铺子账号')

    class Meta:
        verbose_name = u'店铺补充资料'
        verbose_name_plural = verbose_name
        db_table = 'e_store_info'

    def __unicode__(self):
        return self.name

class OrderMemo(models.Model):
    user_id = models.IntegerField(null=True,blank=True,verbose_name=u'用户ID')
    order_id = models.CharField(max_length=50,verbose_name=u'订单ID')
    type = models.IntegerField(choices=((1,u'拣货说明'),(2,u'客服备注')),verbose_name=u'备注类型')
    content = models.TextField(verbose_name=u'内容')
    add_time = models.DateTimeField(auto_now_add=True,verbose_name=u'添加时间')

    class Meta:
        verbose_name = u'订单备注'
        verbose_name_plural = verbose_name
        db_table = 'e_order_memo'

    def __unicode__(self):
        return self.order_id


class OrderDealMark(models.Model):
    user_id = models.IntegerField(null=True,blank=True,verbose_name=u'处理人ID')
    order_id = models.CharField(max_length=50,unique=True,verbose_name=u'订单ID')
    address_check = models.SmallIntegerField(default=0,verbose_name=u'地址是否完善')
    declare_check = models.SmallIntegerField(default=0,verbose_name=u'报关信息完善')
    select_logicstics_time = models.DateTimeField(null=True,blank=True,verbose_name=u'选择物流时间')
    apply_logicstics_no_time = models.DateTimeField(null=True,blank=True,verbose_name=u'申请单号时间')
    virtual_delivery_time = models.DateTimeField(null=True,blank=True,verbose_name=u'虚拟发货时间')
    print_logicstics_time = models.DateTimeField(null=True,blank=True,verbose_name=u'打印面单时间')
    send_goods_time = models.DateTimeField(null=True,blank=True,verbose_name=u'真实发货时间')
    is_shelve = models.SmallIntegerField(default=0,null=True,blank=True,verbose_name=u'搁置状态')
    update_time = models.DateTimeField(auto_now=True,null=True,blank=True,verbose_name=u'更新时间')

    class Meta:
        verbose_name = u'订单处理标记'
        verbose_name_plural = verbose_name
        db_table = 'e_order_deal_mark'

    def __unicode__(self):
        return self.order_id


class ChooseLogistics(models.Model):
    order_id = models.CharField(max_length=50,verbose_name=u'订单ID')
    channel_type = models.CharField(max_length=100,null=True,blank=True,default=None,verbose_name=u'物流渠道类型')
    channel_id = models.CharField(max_length=100,null=True,default=None,verbose_name=u'物流渠道ID')
    channel = models.CharField(max_length=100,verbose_name=u'物流渠道')
    channel_code = models.CharField(max_length=100,null=True,blank=True,default=None,verbose_name=u'物流渠道代码')
    logicstics_no = models.CharField(max_length=50,null=True,blank=True,verbose_name=u'单号')
    lpNumber = models.CharField(max_length=50,null=True,blank=True,verbose_name=u'线上发货标签')
    is_submit = models.SmallIntegerField(default=0,verbose_name=u'是否提交')
    is_success = models.SmallIntegerField(default=0,verbose_name=u'是否请求成功')
    available = models.SmallIntegerField(default=1,verbose_name=u'是否有效')
    choose_time = models.DateTimeField(default=None,null=True,blank=True,verbose_name=u'选择时间')
    isError = models.SmallIntegerField(default=0,verbose_name=u'是否出错')
    errorMsg = models.CharField(max_length=500,null=True,blank=True,verbose_name=u'错误信息')

    class Meta:
        verbose_name = u'选择物流临时表'
        verbose_name_plural = verbose_name
        db_table = 'e_choose_logistics'

    def __unicode__(self):
        return self.order_id


class DeliveryInformation(models.Model):
    type = models.IntegerField(default=1,choices=((1,'正常发货'),(2,'重发包裹'),(3,'换面单')),verbose_name=u'包裹类型')
    order_id = models.CharField(max_length=50,verbose_name=u'订单ID')
    store_id = models.IntegerField(verbose_name=u'店铺ID')
    channel = models.CharField(max_length=100,verbose_name=u'物流渠道')
    channel_code = models.CharField(max_length=100,null=True,default=None,verbose_name=u'物流渠道代码')
    channel_type = models.CharField(max_length=100,null=True,default=None,verbose_name=u'物流渠道类型')
    logicstics_no = models.CharField(max_length=50,verbose_name=u'单号')
    lpNumber = models.CharField(max_length=50,null=True,blank=True,verbose_name=u'线上发货标签')
    logicstics_file = models.CharField(max_length=200,default=None,null=True,verbose_name=u'面单文件')
    logicstics_fee = models.DecimalField(max_digits=5,decimal_places=2,null=True,blank=True,verbose_name=u"物流费用")
    quantity = models.IntegerField(null=True,default=None,verbose_name=u'产品数量')
    memo = models.CharField(max_length=200,null=True,blank=True,verbose_name=u'备注')
    create_time = models.DateTimeField(auto_now_add=True,verbose_name=u'创建时间')
    submit_time = models.DateTimeField(default=None,null=True,blank=True,verbose_name=u'提交发货时间')
    send_time = models.DateTimeField(default=None,null=True,blank=True,verbose_name=u'实际发货时间')
    status = models.IntegerField(null=True,default=None,verbose_name=u'状态')
    is_children = models.IntegerField(default=0,verbose_name=u'是否为拆分的子包裹')

    class Meta:
        verbose_name = u'发货包裹'
        verbose_name_plural = verbose_name
        db_table = 'e_delivery_information'
        unique_together = ("order_id", "logicstics_no")

    def __unicode__(self):
        return self.order_id

class OrderDeal(models.Model):
    orderId = models.CharField(max_length=50,unique=True,verbose_name=u'订单ID')
    platform = models.CharField(max_length=50,null=True,verbose_name=u'平台')
    storeId = models.IntegerField(null=True,verbose_name=u'店铺ID')
    bizType = models.CharField(max_length=50,null=True,verbose_name=u'订单类型')
    isPhone = models.SmallIntegerField(null=True,verbose_name=u'是否手机订单')
    sellerOperatorLoginId = models.CharField(max_length=50,null=True,verbose_name=u'卖家ID')
    sellerSignerFullname = models.CharField(max_length=100,null=True,verbose_name=u'卖家全名')
    gmtCreate = models.DateTimeField(null=True,verbose_name='订单创建时间')
    gmtPaySuccess = models.DateTimeField(null=True,verbose_name='支付成功时间')
    gmtModified = models.DateTimeField(null=True,verbose_name='修改时间')
    gmtSendGoodsTime = models.DateTimeField(null=True,verbose_name='发货时间')
    gmtConfirmReceipt = models.DateTimeField(null=True,verbose_name='确认收货时间')
    gmtTradeEnd = models.DateTimeField(null=True,verbose_name='交易结束时间')
    gmtCheckTime = models.DateTimeField(null=True,verbose_name='最后同步时间')
    orderAmount = models.DecimalField(max_digits=10,decimal_places=2,null=True,verbose_name=u"订单金额")
    orderAmountCurrency = models.CharField(max_length=50,verbose_name=u'订单货币单位')
    paymentType = models.CharField(max_length=50,verbose_name=u'付款方式')
    payAmount = models.DecimalField(max_digits=10,decimal_places=2,null=True,verbose_name=u"付款金额")
    hasRequestLoan = models.SmallIntegerField(null=True,verbose_name=u'是否已请求放款')
    orderStatus = models.CharField(max_length=50,null=True,verbose_name=u'订单状态')
    fundStatus = models.CharField(max_length=50,null=True,verbose_name=u'资金状态')
    frozenStatus = models.CharField(max_length=50,null=True,verbose_name=u'冻结状态')
    issueStatus = models.CharField(max_length=50,null=True,verbose_name=u'纠纷状态')
    refundStatus = models.CharField(max_length=50,null=True,verbose_name=u'退款状态')
    loanStatus = models.CharField(max_length=50,null=True,verbose_name=u'放款状态')
    logisticsStatus = models.CharField(max_length=50,null=True,verbose_name=u'物流状态')
    gmtSendGoodsDeadline = models.DateTimeField(null=True,verbose_name=u'发货期限')
    buyerloginId = models.CharField(max_length=50,null=True,verbose_name=u'买家登录ID')
    buyerSignerFullname = models.CharField(max_length=100,null=True,verbose_name=u'买家全名')
    buyerEmail = models.CharField(max_length=100,null=True,verbose_name=u'买家邮箱')
    buyersChooseLogistics  = models.CharField(max_length=100,null=True,verbose_name=u'买家选择物流')
    contactPerson = models.CharField(max_length=100,null=True,verbose_name=u'收货人名称')
    deliveryAddress = models.CharField(max_length=500,null=True,verbose_name=u'收货地址')
    country = models.CharField(max_length=100,null=True,verbose_name=u'收货国家')
    countryCode = models.CharField(max_length=10,null=True,verbose_name=u'国家代码')
    deliveryLogistics = models.CharField(max_length=200,null=True,verbose_name=u'实际发货物流')
    deliveryLogisticsNo = models.CharField(max_length=500,null=True,verbose_name=u'发货物流单号')
    deliveryLogisticsFee = models.DecimalField(max_digits=10,decimal_places=2,null=True,verbose_name=u"发货物流费用")
    orderProductCost = models.DecimalField(max_digits=10,decimal_places=2,null=True,verbose_name=u"产品采购成本")
    orderTotalCost = models.DecimalField(max_digits=10,decimal_places=2,null=True,verbose_name=u"订单总成本")
    orderProfit = models.DecimalField(max_digits=10,decimal_places=2,null=True,verbose_name=u"订单总利润")
    orderProfitMargin = models.DecimalField(max_digits=5,decimal_places=4,null=True,verbose_name=u"订单利润率")
    orderProductsIds = models.CharField(max_length=500,null=True,verbose_name=u'订单产品ID')
    orderProductsSkus = models.CharField(max_length=500,null=True,verbose_name=u'订单产品SKU')
    orderMemo = models.CharField(max_length=4000,null=True,verbose_name=u'订单备注')
    addressCheck = models.SmallIntegerField(null=True,verbose_name=u'地址是否完整')
    declareCheck = models.SmallIntegerField(null=True,verbose_name=u'报关信息是否完整')
    isOutOfStock = models.SmallIntegerField(null=True,verbose_name=u'是否缺货')
    noMatchingSku = models.SmallIntegerField(null=True,verbose_name=u'无对应SKU')
    isCanMergeOrder = models.SmallIntegerField(null=True,verbose_name=u'可合并订单')
    gmtSelectLogicstics = models.DateTimeField(null=True,verbose_name=u'选择物流时间')
    gmtApplyLogicsticsNo = models.DateTimeField(null=True,verbose_name=u'申请单号时间')
    gmtVirtualDelivery = models.DateTimeField(null=True,verbose_name=u'虚拟发货时间')
    gmtSendDelivery = models.DateTimeField(null=True,verbose_name=u'系统发货时间')
    gmtPrintLogicstics = models.DateTimeField(null=True,verbose_name=u'打印面单时间')
    isShelve = models.SmallIntegerField(default=0,verbose_name=u'搁置订单')
    dealStatus = models.SmallIntegerField(default=0,verbose_name=u'处理状态')
    productCount = models.IntegerField(null=True,blank=True,verbose_name=u'产品数量')
    packageNo = models.CharField(max_length=500,null=True,blank=True,verbose_name=u'包裹号')
    reSend = models.DateTimeField(null=True,verbose_name=u'重发包裹')
    replaceLogicsticsNo = models.DateTimeField(null=True,verbose_name=u'更换面单')
    #0未处理，


    class Meta:
        verbose_name = u'订单处理'
        verbose_name_plural = verbose_name
        db_table = 'e_order_deal'

    def __unicode__(self):
        return self.orderId

class Delivery(models.Model):
    packageNo = models.CharField(max_length=100,unique=True,verbose_name=u'包裹号')
    orderDeal = models.ManyToManyField(OrderDeal,related_name='orderDeal',through='SendOrder',verbose_name=u'处理订单')
    type = models.IntegerField(default=1,choices=((1,'正常发货'),(2,'重发包裹'),(3,'换面单')),verbose_name=u'包裹类型')
    store_id = models.IntegerField(verbose_name=u'店铺ID')
    channel_id = models.CharField(max_length=100,null=True,default=None,verbose_name=u'物流渠道ID')
    channel = models.CharField(max_length=100,verbose_name=u'物流渠道')
    channel_code = models.CharField(max_length=100,null=True,default=None,verbose_name=u'物流渠道代码')
    channel_type = models.CharField(max_length=100,null=True,default=None,verbose_name=u'物流渠道类型')
    logicstics_no = models.CharField(max_length=50,null=True,blank=True,verbose_name=u'单号')
    lpNumber = models.CharField(max_length=50,null=True,blank=True,verbose_name=u'线上发货标签')
    logicstics_file = models.CharField(max_length=200,default=None,null=True,blank=True,verbose_name=u'面单文件')
    logicstics_fee = models.DecimalField(max_digits=5,decimal_places=2,null=True,blank=True,verbose_name=u"物流费用")
    quantity = models.IntegerField(null=True,default=None,verbose_name=u'产品数量')
    memo = models.CharField(max_length=200,null=True,blank=True,verbose_name=u'备注')
    create_time = models.DateTimeField(auto_now_add=True,verbose_name=u'创建时间')
    apply_time = models.DateTimeField(default=None,null=True,blank=True,verbose_name=u'申请单号时间')
    submit_time = models.DateTimeField(default=None,null=True,blank=True,verbose_name=u'提交发货时间')
    send_time = models.DateTimeField(default=None,null=True,blank=True,verbose_name=u'实际发货时间')
    status = models.IntegerField(null=True,default=None,blank=True,verbose_name=u'状态')
    is_children = models.IntegerField(default=0,verbose_name=u'是否为拆分的子包裹')
    is_merge = models.IntegerField(default=0,verbose_name=u'是否为合并发货')
    is_submit = models.SmallIntegerField(default=0,verbose_name=u'是否提交')
    is_success = models.SmallIntegerField(default=0,verbose_name=u'是否请求成功')
    isError = models.SmallIntegerField(default=0,verbose_name=u'是否出错') #0不出错，1申请出错，2发货出错
    errorMsg = models.CharField(max_length=500,null=True,blank=True,verbose_name=u'错误信息')


    class Meta:
        verbose_name = u'运输包裹'
        verbose_name_plural = verbose_name
        db_table = 'e_delivery'

    def __unicode__(self):
        return self.packageNo

class SendOrder(models.Model):
    packageNo = models.ForeignKey(Delivery,to_field='packageNo', verbose_name=u'包裹')
    orderId = models.ForeignKey(OrderDeal,to_field='orderId', verbose_name=u'订单')

    class Meta:
        verbose_name = u'包裹订单关联'
        verbose_name_plural = verbose_name
        db_table = 'e_send_order'

    def __unicode__(self):
        return self.packageNo